home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 376-400 / disk_386 / xlispstat / src2.lzh / XLisp-Stat / xsspin.c < prev   
C/C++ Source or Header  |  1990-10-04  |  13KB  |  480 lines

  1. /* xsscatmat - XLISP interface to IVIEW dynamic graphics package.      */
  2. /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
  3. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  4. /* You may give out copies of this software; for conditions see the    */
  5. /* file COPYING included with this distribution.                       */
  6.  
  7. #include "xlisp.h"
  8. #include "osdef.h"
  9. #ifdef ANSI
  10. #include "xlproto.h"
  11. #include "xlsproto.h"
  12. #include "iviewproto.h"
  13. #include "Stproto.h"
  14. #else
  15. #include "xlfun.h"
  16. #include "xlsfun.h"
  17. #include "iviewfun.h"
  18. #include "Stfun.h"
  19. #endif ANSI
  20. #include "xlsvar.h"
  21.  
  22. /* forward declarations */
  23. #ifdef ANSI
  24. void set_content_variables(LVAL,unsigned,unsigned,unsigned),
  25.      set_cuing(LVAL,int),set_showing_axes(LVAL,int),
  26.      set_rotation_type(LVAL,int),set_angle(LVAL,double),
  27.      setcontrols(Rect *,Rect *,int,int),adjust_cuing(LVAL),
  28.      cuing_off(LVAL),IViewSpinRotate(IVIEW_WINDOW),
  29.      get_content_variables(LVAL,int *,int *,int*),
  30.      get_spin_controls(IVIEW_WINDOW,Rect *,Rect *,Rect *,Rect *,Rect *,Rect *),
  31.      redraw_content(IVIEW_WINDOW,LVAL),
  32.      drawcontrols(StGWWinInfo *,Rect,Rect,char *s);
  33. int is_cuing(LVAL),is_showing_axes(LVAL),rotation_type(LVAL);
  34. double spin_angle(LVAL);
  35. #else
  36. void set_content_variables(),
  37.      set_cuing(),set_showing_axes(),
  38.      set_rotation_type(),set_angle(),
  39.      setcontrols(),adjust_cuing(),
  40.      cuing_off(),IViewSpinRotate(),
  41.      get_content_variables(),
  42.      get_spin_controls(),
  43.      redraw_content(),
  44.      drawcontrols();
  45. int is_cuing(),is_showing_axes()rotation_type();
  46. double spin_angle();
  47. #endif ANSI
  48.  
  49. #define LABEL_OFFSET 3
  50. #define AXIS_FRACTION 0.5
  51.  
  52. #define SPIN_CONTROL_HEIGHT 20
  53. #define SPIN_CONTROL_SIZE 10
  54. #define SPIN_CONTROL_LEFT 5
  55. #define SPIN_CONTROL_TOP 5
  56. #define SPIN_CONTROL_GAP 5
  57. #define SPIN_PITCH_STRING "Pitch"
  58. #define SPIN_ROLL_STRING  "Roll"
  59. #define SPIN_YAW_STRING   "Yaw"
  60. #define ALPHA 3.14159 / 72.0
  61.  
  62. #define Pitching    0
  63. #define Rolling     1
  64. #define Yawing      2
  65. #define ApplyMatrix 3
  66.  
  67. /**************************************************************************/
  68. /**                                                                      **/
  69. /**                      Spinner Creation Functions                      **/
  70. /**                                                                      **/
  71. /**************************************************************************/
  72.  
  73. LVAL iview_spin_allocate()
  74. {
  75.   LVAL object;
  76.   int vars, i, show, ascent, height;
  77.   IVIEW_WINDOW w;
  78.   /*char*/ StGWWinInfo *gwinfo; /* changed JKL */
  79.  
  80.   object = xlgaobject();
  81.   show = xsboolkey(sk_show, TRUE);
  82.   
  83.   gwinfo = StGWObWinInfo(object);
  84.   get_iview_ivars(object, &vars);
  85.   
  86.   if (vars < 3) xlfail("too few variables");
  87.   w = IViewNew(object);
  88.   initialize_iview(w, object);
  89.   
  90.   for (i = 0; i < vars; i++)
  91.     IViewSetScaledRange(w, i, -sqrt((double) vars), sqrt((double) vars));
  92.   set_content_variables(object, 0, 1, 2);
  93.   
  94.   IViewSetIdentityTransformation(w);
  95.   set_rotation_type(object, Rolling);
  96.   set_angle(object, ALPHA);
  97.   ascent = StGWTextAscent(gwinfo);
  98.   height = (ascent > SPIN_CONTROL_SIZE) ? 2 * ascent : SPIN_CONTROL_HEIGHT;
  99.   StGrSetMargin(gwinfo, 0, 0, 0, height);
  100.   
  101.   /* use StShowWindow to show (map) window but NOT send :resize or :redraw */
  102.   if (show) StShowWindow(w);
  103.  
  104.   return(object);
  105. }
  106.  
  107. /**************************************************************************/
  108. /**                                                                      **/
  109. /**                     State Accessors and Mutators                     **/
  110. /**                                                                      **/
  111. /**************************************************************************/
  112.  
  113. LVAL iview_spin_content_variables()
  114. {
  115.   int x, y, z, set = FALSE;
  116.   LVAL object;
  117.   
  118.   object = xlgaobject();
  119.   if (moreargs()) {
  120.     set = TRUE;
  121.     x = getfixnum(xlgafixnum());
  122.     y = getfixnum(xlgafixnum());
  123.     z = getfixnum(xlgafixnum());
  124.   } 
  125.   xllastarg();
  126.   
  127.   if (set) set_content_variables(object, x, y, z);
  128.  
  129.   return(slot_value(object, s_content_variables));
  130. }
  131.  
  132. static void set_content_variables(object, x, y, z)
  133.     LVAL object;
  134.     unsigned x, y, z;
  135. {    
  136.   /*char*/ StGWWinInfo *gwinfo = StGWObWinInfo(object); /* changed JKL */
  137.   StGrSetContentVariables(gwinfo, x, y);
  138.   set_slot_value(object, s_content_variables, integer_list_3(x, y, z));
  139. }
  140.  
  141. static void get_content_variables(object, x, y, z)
  142.     LVAL object;
  143.     int *x, *y, *z;
  144. {
  145.   LVAL val = slot_value(object, s_content_variables);
  146.   if (consp(val) && fixp(car(val))) { 
  147.     if (x != nil) *x = getfixnum(car(val)); 
  148.     val = cdr(val);
  149.   }    
  150.   if (consp(val) && fixp(car(val))) {
  151.     if (y != nil) *y = getfixnum(car(val));
  152.     val = cdr(val);
  153.   }    
  154.   if (consp(val) && fixp(car(val))) {
  155.     if (z != nil) *z = getfixnum(car(val));
  156.   /*  val = cdr(val); not used JKL */
  157.   }    
  158. }
  159.  
  160. LVAL iview_spin_depth_cuing()
  161. {
  162.   LVAL object;
  163.   
  164.   object = xlgaobject();
  165.   if (moreargs()) 
  166.     set_cuing(object, (xlgetarg() != NIL) ? TRUE : FALSE);
  167.   xllastarg();
  168.   
  169.   return((is_cuing(object)) ? s_true : NIL);
  170. }
  171.  
  172. static is_cuing(object)
  173.     LVAL object;
  174. {
  175.   return((slot_value(object, s_depth_cuing) != NIL) ? TRUE : FALSE);
  176. }
  177.  
  178. static void set_cuing(object, cuing)
  179.     LVAL object;
  180.     int cuing;
  181. {
  182.   set_slot_value(object, s_depth_cuing, (cuing) ? s_true : NIL);    
  183.   if (cuing) adjust_cuing(object);
  184.   else cuing_off(object);
  185. }
  186.  
  187. LVAL iview_spin_showing_axes()
  188. {
  189.   LVAL object;
  190.   
  191.   object = xlgaobject();
  192.   if (moreargs())
  193.     set_showing_axes(object, (xlgetarg() != NIL) ? TRUE : FALSE);
  194.   xllastarg();
  195.   
  196.   return((is_showing_axes(object)) ? s_true : NIL);
  197. }
  198.  
  199. static is_showing_axes(object)
  200.     LVAL object;
  201. {
  202.   return((slot_value(object, s_showing_axes) != NIL) ? TRUE : FALSE);
  203. }
  204.  
  205. static void set_showing_axes(object,showing)
  206.     LVAL object;
  207.     int showing;
  208. {
  209.   set_slot_value(object, s_showing_axes, (showing) ? s_true : NIL);    
  210. }
  211.  
  212. static rotation_type(object)
  213.     LVAL object;
  214. {
  215.   LVAL value = slot_value(object, s_rotation_type);
  216.  
  217.   if (symbolp(value)) {
  218.     if (value == s_pitching) return(Pitching);
  219.     else if (value == s_yawing) return(Yawing);
  220.     else return(Rolling);
  221.   }
  222.   else if (matrixp(value)) return(ApplyMatrix);
  223.   else return(Pitching);
  224. }
  225.  
  226. static void set_rotation_type(object, type)
  227.     LVAL object;
  228.     int type;
  229. {
  230.   LVAL value;
  231.  
  232.   switch (type) {
  233.   case Pitching: value = s_pitching; break;
  234.   case Rolling:  value = s_rolling;  break;
  235.   case Yawing:   value = s_yawing;   break;
  236.   default:       value = s_pitching; break;
  237.   }
  238.   set_slot_value(object, s_rotation_type, value);
  239. }
  240.  
  241. static void set_angle(object, alpha)
  242.     LVAL object;
  243.     double alpha;
  244. {
  245.   set_slot_value(object, s_rotation_angle, cvflonum((FLOTYPE) alpha));
  246. }
  247.  
  248. static double spin_angle(object)
  249.     LVAL object;
  250. {
  251.   LVAL value = slot_value(object, s_rotation_angle);
  252.   
  253.   if (floatp(value)) return(getflonum(value));
  254.   else return(0.0);
  255. }
  256.  
  257. LVAL iview_spin_angle()
  258. {
  259.   LVAL object;
  260.   
  261.   object = xlgaobject();
  262.   if (moreargs()) set_angle(object, makedouble(xlgetarg()));
  263.   xllastarg();
  264.   
  265.   return(slot_value(object, s_rotation_angle));
  266. }
  267.  
  268. #ifdef DODO
  269. static void get_spin_controls(w, p1, p2, r1, r2, y1, y2)
  270.     IVIEW_WINDOW w;
  271.     Rect *p1, *p2, *r1, *r2, *y1, *y2;
  272. {
  273.   int height, top, string_space;
  274.   /*char*/ StGWWinInfo *gwinfo = IViewWindowWinInfo(w); /* changed JKL */
  275.   
  276.   height = StGWCanvasHeight(gwinfo);
  277.   string_space = StGWTextWidth(gwinfo, SPIN_PITCH_STRING)
  278.                + SPIN_CONTROL_GAP + 2 * SPIN_CONTROL_SIZE;
  279.                
  280.   top = height - SPIN_CONTROL_HEIGHT + SPIN_CONTROL_GAP;
  281.   setcontrols(p1, p2, SPIN_CONTROL_SIZE, top);
  282.   setcontrols(r1, r2, p2->left + string_space, top);
  283.   setcontrols(y1, y2, r2->left + string_space, top);
  284. }
  285.  
  286. static void setcontrols(r1, r2, x, y)
  287.     Rect *r1, *r2;
  288.     int x, y;
  289. {
  290.   r1->left = x; r1->top = y;
  291.   r1->width = SPIN_CONTROL_SIZE; r1->height = SPIN_CONTROL_SIZE;
  292.   *r2 = *r1;
  293.   r2->left += SPIN_CONTROL_SIZE + SPIN_CONTROL_GAP;
  294. }
  295. #endif DODO
  296.  
  297. /**************************************************************************/
  298. /**                                                                      **/
  299. /**                            Data Functions                            **/
  300. /**                                                                      **/
  301. /**************************************************************************/
  302.  
  303. /**************************************************************************/
  304. /**                                                                      **/
  305. /**                    Drawing and Resizing Functions                    **/
  306. /**                                                                      **/
  307. /**************************************************************************/
  308.  
  309. LVAL iview_spin_resize()
  310. {
  311.   IVIEW_WINDOW w;
  312.   LVAL object;
  313.   int vars, i, top, left, width, height, size;
  314.   /*char*/ StGWWinInfo *gwinfo; /* changed JKL */
  315.   object = xlgaobject();
  316.   xllastarg();
  317.   
  318.   gwinfo = StGWObWinInfo(object);
  319.   w = GETIVIEWADDRESS(object);
  320.   if (w == nil || gwinfo == nil) return(NIL);
  321.   
  322.   vars = IViewNumVariables(w);
  323.   IViewSetFixedAspect(w, TRUE);
  324.   IViewStdResize(w);
  325.   StGrGetContentRect(gwinfo, &left, &top, &width, &height);
  326.   size = width;
  327.   StGrSetContentOrigin(gwinfo, left + size / 2, top + size / 2);
  328.     
  329.   for (i = 0; i < vars; i++)
  330.     IViewSetScreenRange(w, i, - size / 2, size / 2);
  331.   StGWGetViewRect(gwinfo, &left, &top, &width, &height);
  332.   StGWSetClipRect(gwinfo, TRUE, left, top, width, height);
  333.  
  334.   return(NIL);
  335. }
  336.  
  337. static void redraw_content(w, object)
  338.     IVIEW_WINDOW w;
  339.     LVAL object;
  340. {
  341.   int left, top, width, height;
  342.   /*char*/ StGWWinInfo *gwinfo = StGWObWinInfo(object); /* changed JKL */
  343.   
  344.   if (is_cuing(object)) adjust_cuing(object);
  345.   StGWStartBuffering(gwinfo);
  346.   IViewStdRedrawContent(w);
  347.   if (is_showing_axes(object)) send_message(object, sk_draw_axes);
  348.   StGrGetContentRect(gwinfo, &left, &top, &width, &height);
  349.   StGWBufferToScreen(gwinfo, left, top, width, height);
  350. }
  351.  
  352. LVAL iview_spin_draw_axes()
  353. {
  354.   IVIEW_WINDOW w;
  355.   int x, y, xorig, yorig, size, xend, yend, vars, i, unit;
  356.   char *s;
  357.   double **a;
  358.   /*char*/ StGWWinInfo *gwinfo; /* changed JKL */
  359.   LVAL object;
  360.   
  361.   object = xlgaobject();
  362.   w = GETIVIEWADDRESS(object);
  363.   gwinfo = StGWObWinInfo(object);
  364.   if (w != nil) {
  365.     StGrGetContentVariables(gwinfo, &x, &y);
  366.     StGrGetContentOrigin(gwinfo, &xorig, &yorig);
  367.     StGrGetContentRect(gwinfo, nil, nil, &size, nil);
  368.     vars = IViewNumVariables(w);
  369.     a = IViewTransformation(w);
  370.  
  371.     unit = size / 2;
  372.     for (i = 0; i < vars; i++) {
  373.       xend = xorig + a[x][i] * AXIS_FRACTION * unit;
  374.       yend = yorig - a[y][i] * AXIS_FRACTION * unit;
  375.       StGWDrawLine(gwinfo, xorig, yorig, xend, yend);
  376.       s = IViewVariableLabel(w, i);
  377.       xend += LABEL_OFFSET;
  378.       yend -= LABEL_OFFSET;
  379.       StGWDrawString(gwinfo, s, xend, yend);
  380.     }
  381.   }
  382.   return(NIL);
  383. }
  384.  
  385. LVAL iview_spin_redraw_content()
  386. {
  387.   IVIEW_WINDOW w;
  388.   LVAL object;
  389.   
  390.   object = xlgaobject();
  391.   w = GETIVIEWADDRESS(object);
  392.   xllastarg();
  393.   
  394.   if (w != nil) redraw_content(w, object);
  395.   return(NIL);
  396. }
  397.  
  398. #ifdef DODO
  399. static void drawcontrols(gwinfo, r1, r2, s)
  400.     /*char*/ StGWWinInfo *gwinfo; /* changed JKL */
  401.     Rect r1, r2;
  402.     char *s;
  403. {
  404.   int x, y;
  405.   
  406.   x = r2.left + SPIN_CONTROL_GAP + SPIN_CONTROL_SIZE;
  407.   y = r2.top + SPIN_CONTROL_SIZE;
  408.   StGWFrameRect(gwinfo, r1.left, r1.top, r1.width, r1.height);
  409.   StGWFrameRect(gwinfo, r2.left, r2.top, r2.width, r2.height);
  410.   StGWDrawString(gwinfo, s, x, y);
  411. }
  412. #endif DODO
  413.  
  414. /**************************************************************************/
  415. /**                                                                      **/
  416. /**                           Mouse Functions                            **/
  417. /**                                                                      **/
  418. /**************************************************************************/
  419.  
  420. static void adjust_cuing(object)
  421.     LVAL object;
  422. {
  423.   int vx, vy, vz;
  424.   
  425.   get_content_variables(object, &vx, &vy, &vz);
  426.   send_message1(object, sk_adjust_depth_cuing, vz);
  427. }
  428.  
  429. static void cuing_off(object)
  430.     LVAL object;
  431. {
  432.   IVIEW_WINDOW w;
  433.   int n, i;
  434.  
  435.   w = GETIVIEWADDRESS(object);
  436.   if (w == nil) return;
  437.  
  438.   n  = IViewNumPoints(w);
  439.   for (i = 0; i < n; i++) IViewSetPointSymbol(w, i, 0, 5);
  440.   n = IViewNumLines(w);
  441.   for (i = 0; i < n; i++) IViewSetLineWidth(w, i, 1);
  442. }
  443.  
  444. /**************************************************************************/
  445. /**                                                                      **/
  446. /**                         Rotation Functions                           **/
  447. /**                                                                      **/
  448. /**************************************************************************/
  449.  
  450. static void IViewSpinRotate(w)
  451.     IVIEW_WINDOW w;
  452. {
  453.   int x, y, z;
  454.   LVAL object = (LVAL) IViewWindowGetObject(w);
  455.   double alpha = spin_angle(object);
  456.   
  457.   get_content_variables(object, &x, &y, &z);
  458.   
  459.   switch (rotation_type(object)) {
  460.   case Pitching:    IViewRotate2(w, y, z, alpha); break;
  461.   case Rolling:     IViewRotate2(w, x, y, alpha); break;
  462.   case Yawing:      IViewRotate2(w, x, z, alpha); break;
  463.   case ApplyMatrix: send_message_1L(object, 
  464.                                     sk_apply_transformation,
  465.                                     slot_value(object, s_rotation_type));
  466.   }
  467.   IViewRedrawContent(w);
  468. }
  469.  
  470. LVAL iview_spin_rotate()
  471. {
  472.   IVIEW_WINDOW w;
  473.   
  474.   w = GETIVIEWADDRESS(xlgaobject());
  475.   xllastarg();
  476.   
  477.   if (w != nil) IViewSpinRotate(w);
  478.   return(NIL);
  479. }
  480.